package cn.tx.five;

import cn.tx.RabbitMQUtils;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/10/4 17:51
 */
public class Consumer2 {
    @Test
    public void receiveMessage() throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        String exchangeName = "topic";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC);
        //得到一个临时队列
        String queue = channel.queueDeclare().getQueue();
        //绑定队列到交换机
        channel.queueBind(queue,exchangeName,"user.#");
        //消费消息
        channel.basicConsume(queue,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                        AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者【2】收到消息:"+new String(body));
            }
        });
        System.out.println("消费者【2】启动成功");
        System.in.read();
    }
}
